/**
 * Created by LaoXu on 2017/12/21.
 * @description:用户API接口
 */
const express = require('express');
var router = express.Router();
const jwt = require('jsonwebtoken');

const codeCons = require('../../models/constants/codeCons');
const mainCons = require('../../models/constants/mainCons');

const User = require('../../models/user');

var user = new User();

// 根据用户手机号码获取用户的信息
router.post('/userLogin', function (req, res, next) {
    user.userLogin(req.body, function (results) {
        if (results.code == codeCons.SUCCESSFUL) {
            // 生成Token信息
            var token = jwt.sign({userInfo: results.datas}, mainCons.token, {
                expiresIn: 60 * 60 * 24 * 7 // 保存7天
            });
            results.token = token;
            res.send(results);
        } else {
            res.send(results);
        }
    });
});

/**
 * 用户信息注册
 */
router.post('/regUserInfo', function (req, res, next) {
    user.insertUser(req.body, function (results) {
        res.send(results);
    });
});


router.get('/', function (req, res, next) {
    // 获取token值
    // 测试jsonwebtoken解析和使用
    let {token}= req.query;
    jwt.verify(token, mainCons.token, function (err, decoded) {
        console.log(err);
        console.log(decoded);
        res.send(decoded);
    });

});

module.exports = router;